package cn.stronglink.assetmanage.business.task;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.assetlist.AssetListActivity;
import cn.stronglink.assetmanage.business.check.checkorder.CheckOrderAdapter;
import cn.stronglink.assetmanage.business.inventory.view.InventoryDetailActivity;
import cn.stronglink.assetmanage.business.patrol.view.PatrolDetailActivity;
import cn.stronglink.assetmanage.business.repair.assetlist.RepairDetailListActivity;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.listener.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.dao.CheckDataUtil;
import cn.stronglink.assetmanage.utils.dao.TaskDataUtil;

/**
 * 已入库表单列表
 * A simple {@link Fragment} subclass.
 */
public class TaskFinishFragment extends Fragment implements onRecyclerListener {
    private Unbinder mUnbinder;
    @BindView(R.id.rv_task_finish)
    RecyclerView rvChecked;
    private TaskOrderAdapter mOrderAdapter;
    private List<Task> taskList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_task_finish, container, false);
        mUnbinder = ButterKnife.bind(this,view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mOrderAdapter = new TaskOrderAdapter(getActivity());
        mOrderAdapter.setListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayout.VERTICAL,
                false);
        rvChecked.setLayoutManager(layoutManager);
        rvChecked.setAdapter(mOrderAdapter);
        //显示本地数据
        showCheckData();
    }

    private void showCheckData() {
        taskList = TaskDataUtil.getFinishTaskNoDistance();
        if (mOrderAdapter!=null){
            mOrderAdapter.notifyDataChanged(taskList);
        }
    }

    @Override
    public void onItemClicked(View v, int postion) {
        Task task = taskList.get(postion);
        Intent intent =null;
        switch (task.getBusinessType()) {
            case Constants.STATUS_ONE:
                //未入库列表
                intent= new Intent(getActivity(),AssetListActivity.class);
                intent.putExtra(Constants.CHECK_ORDER,task.getId_());
                break;
            case Constants.STATUS_TWO:
                // 未盘点列表
                intent = new Intent(getActivity(), InventoryDetailActivity.class);
                intent.putExtra("inventoryDetail",task.getId_());
                break;
            case Constants.STATUS_THREE:
                //未维修列表
                intent = new Intent(getActivity(), RepairDetailListActivity.class);
                intent.putExtra(Constants.SEARCH_REPAIR_NO,task.getId_());
                break;
            case Constants.STATUS_FOUR:
                //未巡检列表
                intent = new Intent(getActivity(), PatrolDetailActivity.class);
                intent.putExtra("patrolDetail",task.getId_());
                break;
            default:
                break;
        }
        jumpActivity(intent);

    }

    private void jumpActivity(Intent intent) {
        startActivity(intent);
        getActivity().overridePendingTransition(0, 0);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }
}
